package com.jianyun.jyzs.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.jianyun.jyzs.bean.ContactBean;
import com.jianyun.jyzs.bean.User;
import com.jianyun.jyzs.db.DatabaseHelper;
import com.jianyun.jyzs.utils.SysConstant2;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangshijun on 2018/10/9.
 */

public class ContactDao {
    private Dao<ContactBean.ContactUser, Integer> userDao;
    private DatabaseHelper helper;

    public ContactDao(Context context) {
        try{
            helper = DatabaseHelper.getHelper(context);
            userDao = helper.getDao(ContactBean.ContactUser.class);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 获取所有好友
     * @param user
     * @
     */
    public List<ContactBean.ContactUser> queryConstants(String enterpriseCode, String userId) {
        try {
            return userDao.queryBuilder().where().eq("userType", SysConstant2.YES).and().eq("enterpriseCode", enterpriseCode).and().eq("userId", userId).query();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<ContactBean.ContactUser>();
    }
    /**
     * 增加一个用户
     * @param user
     * @
     */
    public void insert(ContactBean.ContactUser user) {
        try {
            userDao.createOrUpdate(user);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    /**
     * 删除一个用户 ById
     * @param user
     * @
     */
    public void delete(ContactBean.ContactUser user) {
        try {
            userDao.delete(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除所有通讯录
     * @
     */
    public void deleteAllContact(String enterpriseCode,String userId) {
        try {

            @SuppressWarnings("rawtypes")
            DeleteBuilder deleteBuilder = userDao.deleteBuilder();
            deleteBuilder.where().eq("userType", SysConstant2.YES).and().eq("enterpriseCode", enterpriseCode).and().eq("userId", userId);
            deleteBuilder.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改一个用户
     * @param user
     * @
     */
    public void update(ContactBean.ContactUser user) {
        try {
            userDao.createOrUpdate(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * serverUserId的用户
     * @return
     * @
     */
    public ContactBean.ContactUser findUserByID(String serverId,String userId,String enterpriseCode) {
        try {
            return userDao.queryBuilder().where().eq("serverUserId", serverId).and().eq("userId", userId).and().eq("enterpriseCode", enterpriseCode).queryForFirst();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
